package com.example.button;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    Button button;
    Button button2;
    Button button3;
    Button button4;

    Button downBtn1,speakBtn1,gtSpeak,gtTimer;

    ProgressBar progressBar;
    ProgressBar progressBar2;

    TextView textView;

    int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button = findViewById(R.id.btn1);
        button2 = findViewById(R.id.btn2);
        button3 = findViewById(R.id.btn3);
        button4 = findViewById(R.id.btn4);
        textView = findViewById(R.id.tv1);
        downBtn1 = findViewById(R.id.down_btn1);
        speakBtn1 = findViewById(R.id.speak_btn1);
        // 跳转到新的页面
        gtTimer = findViewById(R.id.gt_timer);
        gtSpeak = findViewById(R.id.gt_speak);

        progressBar = findViewById(R.id.pb);
        progressBar2 = findViewById(R.id.pb2);

        // 打开定时界面
        gtTimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,TimerActivity.class);
                startActivity(intent);
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"hello world",Toast.LENGTH_LONG).show();
            }
        });

        button.setOnLongClickListener(v->{
            String content = String.format("点击了按钮 %d 次",index++);
            textView.setText(content);
            return true;
        });


        // 影藏
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressBar.setVisibility(ProgressBar.GONE);
            }
        });

        // 显示
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressBar.setVisibility(ProgressBar.VISIBLE);
            }
        });

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int progress = progressBar2.getProgress();
                if(progress >= 100){
                    Toast.makeText(MainActivity.this,"完成了",Toast.LENGTH_SHORT).show();
                }else{
                    progress += 10;
                    progressBar2.setProgress(progress);
                    Toast.makeText(MainActivity.this,"完成"+progress+"%",Toast.LENGTH_SHORT).show();
                }
            }
        });


        Button b1 = findViewById(R.id.btn_1);
        Button b2 = findViewById(R.id.btn_2);
        Button b3 = findViewById(R.id.btn_3);

        b1.setOnClickListener(v->{
            if(!b3.isEnabled())
                b3.setEnabled(true);
        });

        b2.setOnClickListener(v->{
            if(b3.isEnabled())
                b3.setEnabled(false);
        });



        // 创建CountDownTimer实例
        countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // 这个方法会在每次计时器tick时调用，这里更新剩余时间
                timeLeftInMillis = millisUntilFinished;
                updateCountDownText();
            }

            @Override
            public void onFinish() {
                // 这个方法会在倒计时结束时调用
                textView.setText("倒计时结束！");
            }
        };



        /**
         * 倒数一分钟
         */
        downBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 开始倒计时
                countDownTimer.start();

            }
        });

        // 创建TextToSpeech实例
        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    // 设置语言为英语
                    int result = textToSpeech.setLanguage(Locale.US);
//                    result = textToSpeech.setLanguage(Locale.CHINA);

                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        // 语言不支持
                        Toast.makeText(MainActivity.this, "Language not supported", Toast.LENGTH_SHORT).show();
                    } else {
                        // 语言支持，可以开始说话
                        speakOut(textView.getText().toString());
                    }
                } else {
                    // 初始化失败
                    Toast.makeText(MainActivity.this, "TextToSpeech initialization failed", Toast.LENGTH_SHORT).show();
                }
            }
        });


        speakBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                speakOut(textView.getText().toString());

            }
        });


        gtSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SpeakActivity.class);
                startActivity(intent);
            }
        });
    }


    // 将文本转换为语音输出
    private void speakOut(String text) {
        textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    // 更新TextView显示的剩余时间
    private void updateCountDownText() {
        int minutes = (int) (timeLeftInMillis / 1000) / 60;
        int seconds = (int) (timeLeftInMillis / 1000) % 60;

        String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);

        textView.setText(timeLeftFormatted);

        speakOut(timeLeftFormatted);
    }


    // 确保在活动销毁时停止倒计时
    @Override
    protected void onDestroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }

        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onDestroy();
    }

    private CountDownTimer countDownTimer;
    private long timeLeftInMillis = 60000; // 假设我们要设置倒计时时间为1分钟

    private TextToSpeech textToSpeech;
}